﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
namespace DalMgr
{
    public class SftDataMgr
    {
#region IniCfg
        /// <summary>
        /// 获取ini配置文件路径
        /// </summary>
        /// <returns></returns>
        public static string GetCfgPath()
        {
            IniCfg cfg = IniCfg.GetCfg();
            return cfg.cfgPath;
        }
        /// <summary>
        /// 设置ini配置文件路径
        /// </summary>
        /// <param name="iniPath">配置文件路径</param>
        public static void SetCfgPath(string iniPath)
        {
            IniCfg cfg = IniCfg.GetCfg();
            cfg.cfgPath = iniPath;
        }
        /// <summary>
        /// 设置默认配置文件名称
        /// </summary>
        public static void SetDefaultCfgPath()
        {
            IniCfg cfg = IniCfg.GetCfg();
            cfg.SetCfgDefaul();
        }

       /// <summary>
        /// 读取整型
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_def"></param>
        /// <returns></returns>
        public static int ReadInt(string _Section, string _key, int _def = 0)
        {
            IniCfg cfg = IniCfg.GetCfg();
            return cfg.ReadInt(_Section, _key, _def);
        }
        /// <summary>
        /// 读取字符串
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_def"></param>
        /// <returns></returns>
        public static string ReadStr(string _Section, string _key, string _def = "")
        {
            IniCfg cfg = IniCfg.GetCfg();
            return cfg.ReadStr(_Section, _key, _def);
        }
        /// <summary>
        /// 读取Bool值
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_def"></param>
        /// <returns></returns>
        public static bool ReadBool(string _Section, string _key, bool _def = false)
        {
            IniCfg cfg = IniCfg.GetCfg();
            return cfg.ReadBool(_Section, _key, _def);
        }

        /// <summary>
        /// 写入字符串
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static bool WriteCfg(string _Section, string _key, string _value)
        {
            IniCfg cfg = IniCfg.GetCfg();
            return cfg.WriteCfg(_Section, _key, _value);
        }
        /// <summary>
        /// 写入int值
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static bool WriteCfg(string _Section, string _key, int _value)
        {
            IniCfg cfg = IniCfg.GetCfg();
            return cfg.WriteCfg(_Section, _key, _value);
        }
        /// <summary>
        /// 写入Bool值
        /// </summary>
        /// <param name="_Section"></param>
        /// <param name="_key"></param>
        /// <param name="_value"></param>
        /// <returns></returns>
        public static bool WriteCfg(string _Section, string _key, bool _value)
        {
            IniCfg cfg = IniCfg.GetCfg();
            return cfg.WriteCfg(_Section, _key, _value);
        }
#endregion
 

#region ErrLog
        /// <summary>                                                                         
        /// 写入错误Log                                                              
        /// </summary>                                                                        
        /// <param name="ex">异常对象</param>                                                 
        /// <param name="backStr">备用异常消息：当ex为null时有效</param>                      
        /// <returns>异常字符串文本</returns>     
        public static void WriteErrLog(Exception ex, string backStr)
        {
            ErrLog errLog = ErrLog.GetCSftLog();
            string strMsg = errLog.GetExceptionMsg(ex, backStr);
            errLog.WriteLog(strMsg);
            
            SftLog sftLog = SftLog.GetSftLog();
            sftLog.WriteLog(ex.Message);
        }
        /// <summary>
        /// 写入错误Log
        /// </summary>
        /// <param name="str">需要写入的Log</param>
        public static void WriteErrLog(string str)
        {
            ErrLog errLog = ErrLog.GetCSftLog();
            string strMsg = errLog.GetExceptionMsg(null, str);
            errLog.WriteLog(strMsg);

            SftLog sftLog = SftLog.GetSftLog();
            sftLog.WriteLog(str);
        }
#endregion

#region Log
        /// <summary>
        /// 设置消息接受句柄
        /// </summary>
        /// <param name="hwnd"></param>
        public static void SetLogReceiveHwnd(IntPtr hwnd)
        {
            SftLog sftLog = SftLog.GetSftLog();
            sftLog.SetLogReceiveHwnd(hwnd);
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="str"></param>
        public static void WriteLog(string str)
        {
            SftLog sftLog = SftLog.GetSftLog();
            sftLog.WriteLog(str);
        }
        /// <summary>
        /// 升级
        /// </summary>
        public static void HasUpdate()
        {
            SftLog sftLog = SftLog.GetSftLog();
            sftLog.HasUpdate();
        }
#endregion
        /// <summary>
        /// 写入错误和Log信息
        /// </summary>
        /// <param name="ex">错误信息</param>
        /// <param name="backStr">Log信息</param>
        public static void WriteErrAndLog(Exception ex, string backStr)
        {
            WriteLog(backStr);
            WriteErrLog(ex, backStr);
        }
    }
}
